<?php
require_once dirname(__FILE__).'/../../logger.conf';

if(!defined('LOGGER_FILE')){
  define ('LOGGER_FILE',dirname(__FILE__) .'/../log/'); 
}

class Logger {
  
  public static function log($message, $severityType = LOGGER_INFO, $to = LOGGER_FILE, $logDest='') {
    // from logger.conf
    if ( LOGGER_WRITE_OFF ) {
      return ;
    }
    global $logger_logTo, $logger_logFile, $logger_severity;

    if (!is_numeric($severityType)) throw new PAException(INVALID_ID, "Bad logger severity type: $severityType");
    if ($severityType >= $logger_severity) {
      if($to == LOGGER_FILE){
        $to = $logger_logTo;
      }
      if($logDest == ''){
        $logDest = $logger_logFile;
      }
      $logMessage = date("[m/d/y G:i:s] [");
  
      switch ($severityType) {
        case LOGGER_INFO:
          $logMessage .= " INFO: ";
          break;
        case LOGGER_WARNING:
          $logMessage .= " WARNING: ";
          break;
        case LOGGER_ERROR:
          $logMessage .= " ERROR: ";
          break;
      }
      $logMessage .= "]"."[ ".$message."]\n";
      

      // Code to send ERROR type log messages STARTS HERE      
        $additional_str = null;
        if($severityType == LOGGER_ERROR) {
            
            $additional_str.= "<br><font style='color:red;'>Called from Page : </font>".basename($_SERVER['SCRIPT_FILENAME']);
            if(!empty($_SERVER['QUERY_STRING'])) {
                $additional_str.= "<br><font style='color:red;'>Arguments List : </font>".$_SERVER['QUERY_STRING'];
            }
            $additional_str.= "<br><font style='color:red;'>Browser Info : </font>".$_SERVER['HTTP_USER_AGENT'];
            
            $error_mail_to = 'techgigappissues@gmail.com';
            $error_subject = date("Y-m-d")." ".$message;  //  "Application ERROR Occured. Attention required!! - ".date("Y-m-d");
            $error_body = $logMessage."<br />".$additional_str;
            $fromEmail = PP::$site_name_title;
            
            $emailFrom = "no-reply@techgig.com";
            $header = "From: ". $fromEmail . " <" . $emailFrom . ">\r\nContent-type: text/html";
            //ts_mail($error_mail_to, $error_subject, $error_body, $header, ' <no-reply@techgig.com>',null);

		// Log error in Table too for fortnight mailer activity and report display Starts here

		// Ends here
        }
      // Code to send ERROR type log messages ENDS HERE
  
      //log messages only with severity type greater than 
      if(!strstr(PP::$path,$logDest))$logDest = PP::$path . $logDest;
      if (error_log($logMessage, $to, $logDest) == FALSE) {
	      echo "<h1>Fatal error!</h1>";
	      echo "<p><b>An error occurred while trying to write to the log</b>: <code>$php_errormsg</code>.</p>";
	      echo "<p>Check that the log file (<code>$logDest</code>) is writeable by the web server.</p>";
	      echo "<p>Here's the message I was trying to log:</p>";
	      echo "<div style='border: solid 1px black; background-color: red; padding: 1em; font-weight: bold;'><code>$logMessage</code></div>";
	      exit;
      }
    } 
    return;
  }
}
?>